package acwing._1_1AlgorithmBasic;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Project : ProgrammingExercises
 * @Package : ACWing.Learning.AlgorithmTemplate
 * @File : _1_8IntervalMerge.java
 * @Author : WangRuoyu
 * @Date 2023/3/11 11:16
 */

public class _1_8IntervalMerge {
    static int intervalMerge(int[][] q) {
        int res = 0;

        Arrays.sort(q, (o1, o2) -> o1[0] != o2[0] ? o1[0] - o2[0] : o1[1] - o2[1]);
        List<long[]> list = new ArrayList<>();

        long start = -1000000010;
        long end = -1000000010;

        for (int[] ints : q) {
            if (end < ints[0]) {
                list.add(new long[]{start, end});
                res++;
                start = ints[0];
                end = ints[1];
            } else {
                end = Math.max(ints[1], end);
            }
        }

//        for (int[] ints : q) {
//            System.out.println(Arrays.toString(ints));
//        }

        list.remove(0);
        list.add(new long[]{start, end});
        for (long[] longs : list) {
            System.out.println(Arrays.toString(longs));
        }

        return res;
    }
}


